package com.mryao.routing;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ReceiveLogsDirect {
    public static void main (String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.231.132");
        factory.setUsername("admin");
        factory.setPassword("admin");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        //声明exchange
        channel.exchangeDeclare("dir_logs_queue","direct");
        //声明一个随机queue
        String queueName = channel.queueDeclare().getQueue();
        //exchange bind queue
        channel.queueBind(queueName,"dir_logs_queue","info");
        channel.queueBind(queueName,"dir_logs_queue","error");
        channel.queueBind(queueName,"dir_logs_queue","warning");
        System.out.println("customer waiting received message");
        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String (body, "UTF-8");
                System.out.println(" [x] Received '" + message + "'");
                System.out.println(" [x] Done");
            }
        };
        channel.basicConsume(queueName,false,consumer);
    }
}
